#
#  Group controller
#

from dzutil import message, warning
from common.models.groups import *
from common.models.user import Users
from views.group_view import *
from MySQLdb import IntegrityError

#------------------------------------------------------------------------
class GroupController:

    def _index(self):
        return ListView()

    def _show(self,key):
        group = Groups().seek(key)
        return ShowView(group)

    def _edit(self,key):
        group = Groups().seek(key)
        return EditView(group)

    def _confirm_delete(self,key):
		return DeleteView(Groups().seek(key))

    def _remove_supergroup(self,key,groupid):
        group = Groups().seek(groupid)
        group.remove_supergroup(key)
        message('Removed "%s"' % Groups().seek(key).NAME)
        return ShowView(group)

    def _add_supergroup(self,groupid,supergroup_name):
        group = Groups().seek(groupid)
        try:
            groupid = Groups().find_by_name(supergroup_name).GROUPID
        except:
            warning('Unknown "%s"' % supergroup_name)
        else:
            group.add_supergroup(groupid)
            message('Added "%s"' % supergroup_name)
            return ShowView(group)
        return ShowView(group)

    def _remove_subgroup(self,key,groupid):
        group = Groups().seek(groupid)
        group.remove_subgroup(key)
        message('Removed "%s"' % Groups().seek(key).NAME)
        return ShowView(group)

    def _add_subgroup(self,groupid,subgroup_name):
        group = Groups().seek(groupid)
        try:
            groupid = Groups().find_by_name(subgroup_name).GROUPID
        except:
            warning('Unknown "%s"' % subgroup_name)
        else:
            group.add_subgroup(groupid)
            message('Added "%s"' % subgroup_name)
            return ShowView(group)
        return ShowView(group)

    def _remove_user(self,key,groupid):
        group = Groups().seek(groupid)
        group.remove_member(key)
        message('Removed "%s"' % Users().seek(key).LOGINID)
        return ShowView(group)

    def _add_user(self,groupid,loginid):
        group = Groups().seek(groupid)
        try:
            userid = Users().find_by_loginid(loginid).USERID
        except:
            message('Unknown "%s"' % loginid)
        else:
            try:
                group.add_member(userid)
            except:
                message('Error adding user "%s"' % loginid)
            else:
                message('Added user "%s"' % loginid)
            #return ShowView(group)
        return ShowView(group)

    def _delete(self,GROUPID,**args):
        groups = Groups()
        groups.delete(GROUPID)
        message('Deleted group')
        return ListView()

    def _add(self):
        return AddView()

    def _update(self,GROUPID,**args):
        group = Groups().seek(GROUPID)
        group.update(args)
        try:
            group.save()
        except IntegrityError:
            message('Group exists')
            return EditView(group)
        message('Updated')
        return ShowView(group)

    def _insert(self,**args):
        new_group = Group()
        new_group.update(args)
        groups = Groups()
        try:
            newid = groups.insert(new_group)
        except IntegrityError:
            message('Data integrity error')
            return AddView(new_group)
        message('New group saved')
        return self._show(newid)


result = GroupController()

